﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace mamnontuoitho7.AppCode
{
    public class VideoClip
    {
        public ConnectGetData _connectData;
        public VideoClip()
        {
            _connectData = new ConnectGetData();
        }

        public bool VideoCreateBySP(String ProductTypeID, String MemberID, String Title, String Introduce, String ProductStatusID, String ViewNumber, String PostUser, String videoURL)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                SqlCommand command = new SqlCommand("sp_VideoInsert", con);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ProductTypeID", SqlDbType.NVarChar).Value = ProductTypeID;
                command.Parameters.Add("@MemberID", SqlDbType.NVarChar).Value = MemberID;
                command.Parameters.Add("@Title", SqlDbType.NVarChar).Value = Title;
                command.Parameters.Add("@Introduce", SqlDbType.NVarChar).Value = Introduce;
                command.Parameters.Add("@ProductStatusID", SqlDbType.NVarChar).Value = ProductStatusID;
                command.Parameters.Add("@ViewNumber", SqlDbType.NVarChar).Value = ViewNumber;
                command.Parameters.Add("@PostUser", SqlDbType.NVarChar).Value = PostUser;
                command.Parameters.Add("@VideoURL", SqlDbType.NVarChar).Value = videoURL;

                command.ExecuteNonQuery();
                con.Dispose();
                con.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("SQL Error" + ex.Message.ToString());
                return false;
            }
            return true;
        }

        /// <summary>
        /// get all video for admin or user
        /// </summary>
        /// <returns>datatable</returns>
        public DataTable GetVideo(String role, String amount)
        {
            string str = "";
            if (role.Equals("admin"))
            {
                if (amount == "0")
                {
                    str = "select  * from VideoView ORDER BY ProductID DESC";
                }
                else 
                {
                    str = "select top(" + amount + ") * from VideoView ORDER BY ProductID DESC";
                }
            }
            else
            {
                str = "select  * from VideoView where ProductStatusID = 0 ORDER BY ProductID DESC";
            }
            return _connectData.GetDataTable(str);
        }

        /// <summary>
        /// get some latest video for user
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public DataTable GetSomeVideo(String number) 
        {
            string str = "select top("+number+")  * from VideoView where ProductStatusID = 0 ORDER BY ProductID DESC";
            return _connectData.GetDataTable(str);
        }


        /// <summary>
        /// cut string video link (youtube)
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public String CutVideoURL(String str)
        {
            if (str.IndexOf("&") == -1)
            {
                return str.Substring(str.IndexOf("watch?v=") + 8);
            }
            else
            {
                return str.Substring(str.IndexOf("watch?v=") + 8, str.IndexOf("&") - (str.IndexOf("watch?v=") + 8));
            }
        }

        /// <summary>
        /// update video 
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="title"></param>
        /// <param name="introduce"></param>
        /// <param name="editUser"></param>
        /// <returns></returns>
        public bool VideoUpdate(String productID, String title, String introduce, String editUser,String videoURL)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sql = "update Product set Title = @Title, Introduce = @Introduce, [EditDate] = default, [EditUser] = @edituser where ProductID = @ProductID";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ProductID", productID);
                cmd.Parameters.AddWithValue("@Title", title);
                cmd.Parameters.AddWithValue("@Introduce", introduce);
                cmd.Parameters.AddWithValue("@edituser", editUser);

                cmd.ExecuteNonQuery();
                cmd.Dispose();


                string sql2 = "update Video set VideoURL = @VideoURL where VideoID = @VideoID";
                SqlCommand cmd2 = new SqlCommand(sql2, con);
                cmd2.Parameters.AddWithValue("@VideoURL", videoURL);
                cmd2.Parameters.AddWithValue("@VideoID", productID);

              
                cmd2.ExecuteNonQuery();
                cmd2.Dispose();

                con.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// get album by id for user, page AlbumPhoto.aspx in module
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public DataTable GetVideoByID(String productID)
        {

            SqlConnection con = _connectData.GetConnect();
            //        con.Open();

            string sql = "select * from VideoView where ProductID = @ProductID and ProductStatusID=0";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@ProductID", productID);
            DataTable dt = _connectData.GetDataTable(cmd);
            //         con.Close();
            return dt;
        }
    }
}